package com.trex.modules.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ArrayNode;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class JsonGeneratorDetail {

    public static String createJson(Map<String, Object> sysInfo, List<Map<String, Object>> tables) throws Exception {
        ObjectMapper mapper = new ObjectMapper();

        // 创建根节点
        ObjectNode root = mapper.createObjectNode();

        // 构建 SysInfo 节点
        ObjectNode sysInfoNode = mapper.createObjectNode();
        sysInfo.forEach((key, value) -> sysInfoNode.put(key, value.toString()));
        root.set("SysInfo", sysInfoNode);

        // 构建 Tables 节点
        ArrayNode tablesArray = mapper.createArrayNode();
        for (Map<String, Object> tableData : tables) {
            ObjectNode tableNode = mapper.createObjectNode();
            tableNode.put("Name", (String) tableData.get("Name"));

            // 构建 Columns 节点
            ArrayNode columnsArray = mapper.createArrayNode();
            List<Map<String, String>> columns = (List<Map<String, String>>) tableData.get("Columns");
            for (Map<String, String> column : columns) {
                ObjectNode columnNode = mapper.createObjectNode();
                column.forEach(columnNode::put);
                columnsArray.add(columnNode);
            }
            tableNode.set("Columns", columnsArray);

            // 构建 Rows 节点
            ArrayNode rowsArray = mapper.createArrayNode();
            List<List<Object>> rows = (List<List<Object>>) tableData.get("Rows");
            for (List<Object> row : rows) {
                ArrayNode rowArray = mapper.createArrayNode();
                for (Object value : row) {
                    if (value instanceof String) {
                        rowArray.add((String) value);
                    } else if (value instanceof Integer) {
                        rowArray.add((Integer) value);
                    } else if (value instanceof Double) {
                        rowArray.add((Double) value);
                    }else if (value instanceof Date) {
                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                        rowArray.add(dateFormat.format((Date) value));
                    }
                }
                rowsArray.add(rowArray);
            }
            tableNode.set("Rows", rowsArray);

            tablesArray.add(tableNode);
        }
        root.set("Tables", tablesArray);

        // 生成 JSON 字符串
        return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root);
    }

    public static void main(String[] args) throws Exception {
        // SysInfo 数据
        Map<String, Object> sysInfo = Map.of(
                "Flag", 0,
                "Msg", "",
                "SvcName", "",
                "UUID", "217ede454b284142817aea472593feec"
        );

        // BODY 表的 Columns 数据
        List<Map<String, String>> bodyColumns = List.of(
                Map.of("Name", "OPERATION_FLAG", "DataType", "S", "Caption", "I:新增;U:修改;D:删除"),
                Map.of("Name", "PLAN_NO", "DataType", "S", "Caption", "计划号"),
                Map.of("Name", "RAILWAY_NO", "DataType", "S", "Caption", "股道代码"),
                Map.of("Name", "RAILWAY_TIME", "DataType", "S", "Caption", "离道时刻"),
                Map.of("Name", "RAILWAY_NUM", "DataType", "I", "Caption", "车皮数（循环次数）")
        );

        // BODY 表的 Rows 数据
        List<List<Object>> bodyRows = List.of(
                List.of("I", "E5RP2408110005", "0101310008", "20240811224509", 1)
        );

        // DETAIL 表的 Columns 数据
        List<Map<String, String>> detailColumns = List.of(
                Map.of("Name", "RAILWAY_SEQ", "DataType", "I", "Caption", "顺位"),
                Map.of("Name", "WAGON_NO", "DataType", "S", "Caption", "车皮号"),
                Map.of("Name", "WAGON_ID", "DataType", "S", "Caption", "车皮号ID")
        );

        // DETAIL 表的 Rows 数据
        List<List<Object>> detailRows = List.of(
                List.of(25, "4895509", "EB0DA683E88B4A6CAC984BED7B58582A")
        );

        // 构建 Tables 数据
        List<Map<String, Object>> tables = List.of(
                Map.of("Name", "BODY", "Columns", bodyColumns, "Rows", bodyRows),
                Map.of("Name", "DETAIL", "Columns", detailColumns, "Rows", detailRows)
        );

        // 生成 JSON
        String json = createJson(sysInfo, tables);
        System.out.println(json);
    }
}
